public class Solution978 {
    public int maxTurbulenceSize(int[] arr) {
        int ans=0;
        int last=0;
        int flag=0;
        for (int i=0;i<arr.length-1;i++){
            if (last%2==1 && (arr[i]-arr[i+1])*flag<0 || last%2==0 && (arr[i]-arr[i+1])*flag>0) {
                last++;
            }else{
                ans=Math.max(ans,last);
                last=0;
                flag=0;
            }
            if (last==0 && arr[i]!=arr[i+1]){
                last++;
                flag=arr[i]-arr[i+1]>0?1:-1;
            }
        }
        return Math.max(ans,last)+1;
    }

    public static void main(String[] args) {
        System.out.println(new Solution978().maxTurbulenceSize(new int[]{1,2,3,4,3,4,3,4,3,4}));
    }
}

